clear

cd "C:\Users\maschaff\OneDrive\Documents\Research\banking\April 2019"

*Load Data
use "data_geo.dta", clear

*****************************************************************************
*** Setup: see Kashyap & Stein (2000) and/or Den Haan, Sumner, Yamashiro (2002) for details ***

*Drop non-states 
drop if rssd9200 == "0"
keep if  rssd9210>0 & rssd9210< 57

*Drop Delaware and/or South Dakota (see Jayaratne & Strahan 1998 JLE)
*drop if rssd9210 == 10
*drop if rssd9210 == 46

*Sample period
keep if year_q < tq(1995q1)

*Positive Assets
keep if rcfd2170 > 0

*Commercial Banks Only
keep if rssd9048 == 200 

*Insured
*keep if rssd9424 == 1 | rssd9424 == 2 | rssd9424 == 6
drop if rssd9424 == 0

*Merger
drop if MERGE_CD != .

*Set panel data
xtset rssd9001 year_q

*Fix Total Loans pre-1984 (insert before calcuating loans2 above)
gen loans2 = rcfd1400
replace loans2 = rcfd1400+rcfd2165 if year_q < tq(1984q1)

*Put loans and loan components in real terms
gen c_p_i = cpi/100
gen r_loans = loans2/c_p_i 
gen r_ci = rcfd1600/c_p_i
gen r_rlest = rcfd1410/c_p_i
gen r_con = rcfd1975/c_p_i
gen r_ag = rcfd1590/c_p_i

*Log difference 
gen loans = ln(r_loans) - ln(L.r_loans)
gen ci = ln(r_ci) - ln(L.r_ci) 
gen re = ln(r_rlest) - ln(L.r_rlest) 
gen con = ln(r_con) - ln(L.r_con)
gen ag = ln(r_ag) - ln(L.r_ag) 

*Drop outliers
sort year_q
by year_q: egen sdv = sd(loans)
drop if loans >= 5*sdv
drop if loans <= -5*sdv

*Loan Component Outliers
gen ci_share = rcfd1600/loans2
*drop if ci_share < 0.05
gen re_share = rcfd1410/loans2
*drop if re_share < 0.05
gen con_share = rcfd1975/loans2
*drop if con_share < 0.05
gen ag_share = rcfd1590/loans2
*drop if ag_share < 0.05

*Four Consecutive Quarters of Loan Growth
ssc install tsspell
tsspell, c(loans > 0)
sort rssd9001
by rssd9001: egen maxrun = max(_seq)
drop if maxrun < 4

*Percentiles
sort year_q
by year_q: egen p95 = pctile(rcfd2170), p(95)
by year_q: egen p99 = pctile(rcfd2170), p(99)

*Generate Quarter/Year/timetrend Series
sort rssd9001 year_q
gen Quarter = quarter(date2)
gen Year = year(date2) 
egen time = group(year_q)

*Choose to keep (or drop) year deregulation was implemented 
*replace intra = 1 if intra == .
*replace inter =1 if inter == .

********** Pre-Deregulation Dummy ********************************************** 
tsset rssd9001 year_q

gen dereg = 0
replace dereg = 1 if inter == .
*replace dereg = 0 if dereg == . & inter[_n-1] == 1 & inter[_n+1] == 1

gen yearof = 0
replace yearof = 1 if dereg == 1

sort rssd9001 year_q
gen post1 = 0
by rssd9001: replace post1 = 1 if L4.dereg == 1 & dereg == 0| L3.dereg == 1 & dereg == 0 | L2.dereg == 1 & dereg == 0 | L.dereg == 1 & dereg == 0

sort rssd9001 year_q
gen post2 = 0
by rssd9001: replace post2 = 1 if L4.post1 == 1 & post1 == 0

sort rssd9001 year_q
gen post3 = 0
by rssd9001: replace post3 = 1 if L4.post2 == 1 & post2 == 0 

sort rssd9001 year_q
gen pre1 = 0
replace pre1 = 1 if F4.dereg == 1 & dereg == 0| F3.dereg == 1 & dereg == 0 | F2.dereg == 1 & dereg == 0 | F.dereg == 1 & dereg == 0

sort rssd9001 year_q
gen pre2 = 0
by rssd9001: replace pre2 = 1 if F4.pre1 == 1 & pre1 == 0

sort rssd9001 year_q
gen pre3 = 0
by rssd9001: replace pre3 = 1 if F4.pre2 == 1 & pre2 == 0 

sort rssd9001 year_q 
xtset rssd9001 year_q 

gen pre_event = 0
replace pre_event = 1 if inter == 0
replace pre_event = 0 if pre1 == 1 | pre2 == 1 | pre3 == 1 
gen post_event = 0
replace post_event = 1 if inter ==1 
replace post_event = 0 if yearof == 1 | post1 == 1 | post2 == 1 | post3 == 1 
********************************************************************************

gen rr_pre2  = rr*pre2
gen rr1_pre2 = L1.rr*pre2
gen rr2_pre2 = L2.rr*pre2
gen rr3_pre2 = L3.rr*pre2
gen rr4_pre2 = L4.rr*pre2

gen rr_pre3  = rr*pre3
gen rr1_pre3 = L1.rr*pre3
gen rr2_pre3 = L2.rr*pre3
gen rr3_pre3 = L3.rr*pre3
gen rr4_pre3 = L4.rr*pre3

gen rr_pre_event  = rr*pre_event
gen rr1_pre_event = L1.rr*pre_event
gen rr2_pre_event = L2.rr*pre_event
gen rr3_pre_event = L3.rr*pre_event
gen rr4_pre_event = L4.rr*pre_event

gen rr_yo  = rr*yearof
gen rr1_yo = L1.rr*yearof
gen rr2_yo = L2.rr*yearof
gen rr3_yo = L3.rr*yearof
gen rr4_yo = L4.rr*yearof

gen rr_post1  = rr*post1
gen rr1_post1 = L1.rr*post1
gen rr2_post1 = L2.rr*post1
gen rr3_post1 = L3.rr*post1
gen rr4_post1 = L4.rr*post1

gen rr_post2  = rr*post2
gen rr1_post2 = L1.rr*post2
gen rr2_post2 = L2.rr*post2
gen rr3_post2 = L3.rr*post2
gen rr4_post2 = L4.rr*post2

gen rr_post3  = rr*post3
gen rr1_post3 = L1.rr*post3
gen rr2_post3 = L2.rr*post3
gen rr3_post3 = L3.rr*post3
gen rr4_post3 = L4.rr*post3

gen rr_post_event  = rr*post_event
gen rr1_post_event = L1.rr*post_event
gen rr2_post_event = L2.rr*post_event
gen rr3_post_event = L3.rr*post_event
gen rr4_post_event = L4.rr*post_event

********************************************************************************'
* +/-4 time periods drop pre1
local interactions4 pre_event pre3 pre2 yearof post1 post2 post3 post_event rr_post_event rr1_post_event rr2_post_event rr3_post_event rr4_post_event rr_pre_event rr1_pre_event rr2_pre_event rr3_pre_event rr4_pre_event rr_pre3 rr1_pre3 rr2_pre3 rr3_pre3 rr4_pre3 rr_pre2 rr1_pre2 rr2_pre2 rr3_pre2 rr4_pre2 rr_yo rr1_yo rr2_yo rr3_yo rr4_yo rr_post3 rr1_post3 rr2_post3 rr3_post3 rr4_post3 rr_post2 rr1_post2 rr2_post2 rr3_post2 rr4_post2 rr_post1 rr1_post1 rr2_post1 rr3_post1 rr4_post1      

xtreg loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg `interactions4' i.year_q, fe vce(cluster rssd9210) nonest
lincom rr_pre_event + rr1_pre_event + rr2_pre_event + rr3_pre_event + rr4_pre_event
lincom rr_pre3 + rr1_pre3 + rr2_pre3 + rr3_pre3 + rr4_pre3
lincom rr_pre2 + rr1_pre2 + rr2_pre2 + rr3_pre2 + rr4_pre2
lincom rr_yo + rr1_yo + rr2_yo + rr3_yo + rr4_yo 
lincom rr_post1 + rr1_post1 + rr2_post1 + rr3_post1 + rr4_post1
lincom rr_post2 + rr1_post2 + rr2_post2 + rr3_post2 + rr4_post2
lincom rr_post3 + rr1_post3 + rr2_post3 + rr3_post3 + rr4_post3
lincom rr_post_event + rr1_post_event + rr2_post_event + rr3_post_event + rr4_post_event
